<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/17
 * Time: 上午10:00
 */

namespace app\common\model;

use think\Cache;
use \think\Model;
use think\Db;

/*************************************设置的KEY**************************************/

//短信设置
define("H_SETTINGS_ACCOUNT_SMS", "key_account_sms");
//推送设置
define("H_SETTINGS_ACCOUNT_PUSH", "key_account_push");
//推广海报
define("H_SETTINGS_POSTER_SPREAD", "key_spread_poster");
//用户协议
define("H_SETTINGS_TREATY", "key_treaty");
//关于我们
define("H_SETTINGS_ABOUT_US", "key_about_us");
//优惠券解释权
define("H_SETTINGS_DISCOUNT_EXPLAIN", "key_discount_explain");


//启动广告
define("H_ADVERT_START", 8);



class Settings extends Common
{
    /**联系客服设置
     *
     * @return mixed|null
     */
    public static function contactSettings()
    {
        $settings = self::getSettings("key_contact");

        //QQ
        if (!array_key_exists("qq", $settings))
        {
            $settings["qq"] = "876890112";
        }

        //联系电话
        if (!array_key_exists("tel", $settings))
        {
            $settings["tel"] = "4006337188";
        }

        return $settings;
    }
    /**
     * 获取指定的设置
     *
     * @param $key
     * @return mixed|null
     */
    public static function getSettings($key)
    {
        //初始化设置值
        $settings = null;

        //判断是否存在缓存
        if (Cache::store("file")->has("system_settings"))
        {
            //获取缓存
            $settingsCache = Cache::store("file")->get("system_settings");

            //判断是否存在
            if (array_key_exists($key, $settingsCache))
            {
                $settings = $settingsCache[$key];
            }
        }

        //如果没有缓存
        if (is_null($settings))
        {
            //从数据库中检索数据
            $item = Db::name("system_settings")
                ->where("key_name", $key)
                ->find();

            //如果设置为空
            if (empty($item) || empty($item["value"]))
            {
                $settings = array();
            }
            else
            {
                //反串行化
                $settings = unserialize($item["value"]);

                $settingsCache[$key] = $settings;

                Cache::store("file")->set("system_settings", $settingsCache);
            }
        }

        return $settings;

    }

    /**
     * 设置系统设置值
     *
     * @param string $key 设置的键值
     * @param array $value 设置的数据
     * @return bool|int|string
     */
    static public function setSettings($key, $value)
    {
        //设置结果
        $result = false;

        //获取item
        $item = Db::name("system_settings")
            ->where("key_name", $key)
            ->find();

        //如果存在设置项，则更新值
        if ($item)
        {
            $result = Db::name("system_settings")
                ->where("key_name", $key)
                ->setField("value", serialize($value));
        }
        //否则创建设置
        else
        {
            $result = Db::name("system_settings")
                ->insert(array(
                    "key_name" => $key,
                    "value" => serialize($value),
                ));
        }

        //获取设置缓存
        $settingsCache = Cache::store("file")->has("system_settings")
            ? Cache::store("file")->get("system_settings") : array();
        //更新缓存
        $settingsCache[$key] = $value;
        Cache::store("file")->set("system_settings", $settingsCache);

        return $result;
    }

    /*
     * 系统参数
     * **/
    static public function getSystem()
    {
        $rate=Db::name('rate')
            ->where('id',1)
            ->find();
        return $rate;
    }
}
